package net.xdclass.config;

import org.springframework.context.annotation.Configuration;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 功能描述
 *
 * @author ASUS
 * @date 2024/07/18  9:17
 */
@Configuration
public class SnowFlakeWordIdConfig {
    static {
        try {
            InetAddress inetAddress = Inet4Address.getLocalHost();
            String hostAddressIp = inetAddress.getHostAddress();
            //workid最大值1<<10就是1024
            String workerId = Math.abs(hostAddressIp.hashCode() % 1024) +"";
            System.setProperty("workerId",workerId);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        InetAddress inetAddress = null;
        try {
            inetAddress = Inet4Address.getLocalHost();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        System.out.println(inetAddress.getHostAddress());
        System.out.println(inetAddress.getHostName());
        String workId = Math.abs(inetAddress.getHostAddress().hashCode() % 1024) +"";
        System.out.println(workId);
    }
}

